package com.hctforgreen.greenservice;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.taptwo.android.widget.MyXListView;
import org.taptwo.android.widget.MyXListView.IXListViewListener;

import com.hctforgreen.greenservice.R.id;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.AttrEntity;
import com.hctforgreen.greenservice.model.MessageTopFirstEntity;
import com.hctforgreen.greenservice.model.MessageTopThirdEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.model.StudyEntity;
import com.hctforgreen.greenservice.ui.adapter.RecentTerminalDataListAdapter;
import com.hctforgreen.greenservice.ui.adapter.StudyListAdapter;
import com.hctforgreen.greenservice.ui.adapter.TerminalStudyListAdapter;
import com.hctforgreen.greenservice.ui.handler.RecentTerminalDataListHandler;
import com.hctforgreen.greenservice.ui.widget.DefaultSeriesListPopupView;
import com.hctforgreen.greenservice.ui.widget.LearningTypeListPopupView;
import com.hctforgreen.greenservice.ui.widget.MessageTopFirstPopupView;
import com.hctforgreen.greenservice.ui.widget.MessageTopThirdPopupView;
import com.hctforgreen.greenservice.ui.widget.TerminalDataTimePopupView;
import com.hctforgreen.greenservice.ui.widget.TerminalLearningTypeListPopupView;
import com.hctforgreen.greenservice.utils.CheckDataVersionCodeUtil;
import com.hctforgreen.greenservice.utils.CheckDataVersionCodeUtil.CheckDataVersionCodeTask;
import com.hctforgreen.greenservice.utils.DataVersionCodeUtil;
import com.hctforgreen.greenservice.utils.DownloadBookLstInitFinishUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.KeyboardUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author 390694 终端培训--学习资料
 */
public class TerminalLearningMaterialsActivity extends ParentActivity {

	/**
	 * 第一个下拉框--资料类别
	 */
	private TerminalLearningTypeListPopupView mTypeListPopupView;
	private Button seriesBooksBtn;
	/**
	 * 第二个下拉框--子类别
	 */
	private TerminalLearningTypeListPopupView mChildTypeListPopupView;
	private Button childSeriesBooksBtn;
	/**
	 * 第三个下拉框---时间
	 */
	private TerminalDataTimePopupView mThirdPopupView;
	private Button timeBooksBtn;
	/**
	 * 资料类别实体类
	 */
	private AttrEntity mCurrentFirstEntity;
	/**
	 * 子类别实体类
	 */
	private AttrEntity mCurrentSecondEntity;
	/**
	 * 时间
	 */
	private MessageTopThirdEntity mSelectTimeEntity;

	View convertView;
	// 最近上传学习资料的ListView
	ListView recentListView;

	// 关键字
	private EditText keyWordEditText;
	// 主题搜索
	private EditText themeEditText;
	// 搜索
	private Button searchBtn;
	
	private TerminalLearningMaterialsActivity mSelf;
	private boolean bShowDataLst = false;
	
	private LinearLayout loadLyt;
	private LinearLayout reloadLyt;
	private Button reloadBtn;
	private TextView loadTv;
	private LinearLayout lyt_default_content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_terminal_learning_materials);
		mSelf = this;
		initSkinLayout();
		
		/**
		 * 2016-11-5 390694  检查基础数据包版本，刷新布局
		 */
		initViewForRefresh();
		updateBaseDataTips();
		
		myFindViewById();
		initTitleButtonBar();
		initWindow();
		initClickEdgehide();
	}
	
	/**
	 * 2016-11-5 390694  检查基础数据包版本，刷新布局
	 */
	private void initViewForRefresh(){
		lyt_default_content = (LinearLayout) findViewById(R.id.lyt_default_content);
		loadLyt = (LinearLayout) findViewById(R.id.lyt_default_load);
		reloadLyt = (LinearLayout) findViewById(R.id.lyt_default_reload);
		reloadBtn = (Button) findViewById(R.id.btn_default_reload);
		loadTv = (TextView) findViewById(R.id.tv_default_load);
	}
	

	/**
	 * 2016-11-5 390694  检查基础数据包版本，提示区更新
	 */
	public void updateBaseDataTips() {
		CheckDataVersionCodeUtil checkDataVersionCodeUtil = new CheckDataVersionCodeUtil();

		checkDataVersionCodeUtil.new CheckDataVersionCodeTask(
				R.string.terminal_update_data_content,R.string.terminal_update_data_title, mSelf, true,
				null, null, null, null, null)
				.execute(mSelf);
	}
	
	private void myFindViewById() {
		convertView = findViewById(R.id.lyt_parent);
		seriesBooksBtn = (Button) findViewById(R.id.btn_series);
		childSeriesBooksBtn = (Button) findViewById(R.id.btn_child_series);
		timeBooksBtn = (Button) findViewById(R.id.btn_select_time);
		keyWordEditText = (EditText) findViewById(R.id.et_keyWord_input);
		themeEditText = (EditText) findViewById(R.id.et_theme_input);
		searchBtn = (Button) findViewById(R.id.btn_search);
		
		recentListView = (ListView) convertView
				.findViewById(R.id.lst_default_list);

	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.train_learning_materials));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();

			}
		});
	}

	private void initWindow() {	
		
		initRecentDataList();
		
		initLearningTypePopupView();
		initChildTypeListPopupView();
		initTerminalDataTimePopupView();

		initSearchBtn();
	}

	/**
	 * 搜索最近上传资料
	 */
	private void initRecentDataList() {
		RecentTerminalDataListAdapter mRecentTerminalDataListAdapter;

		RecentTerminalDataListHandler handler = new RecentTerminalDataListHandler(
				this, convertView);
		mRecentTerminalDataListAdapter = new RecentTerminalDataListAdapter(
				recentListView, this, R.layout.layout_loading,
				R.layout.layout_reloading, handler);

	}

	/**
	 * 初始化搜索
	 */
	private void initSearchBtn() {

		searchBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				KeyboardUtil keyboardUtil = new KeyboardUtil();
				keyboardUtil.hideSoftKeyboard(v);

				/**
				 * 2016-12-31 390694 增加判断更新资料的服务是否在进行，是  则提示
				 */
				boolean isServiceRunning = DownloadBookLstInitFinishUtil.getUpdateBookServiceFinished(mSelf);
				if (isServiceRunning){
					Dialog dialog = null;
					AlertDialog.Builder builder = new AlertDialog.Builder(mSelf);
					builder.setTitle(R.string.dialog_default_title_hint)
							.setMessage(R.string.update_now)
							.setPositiveButton(R.string.dialog_confirm_hint,
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(DialogInterface dialog,
															int which) {
											dialog.dismiss();
										}
									});
					dialog = builder.create();
					dialog.show();
					return;
				}

				String keyWordString = keyWordEditText.getText().toString()
						.trim();
				String themeWordString = themeEditText.getText().toString()
						.trim();
				
				Intent intent = new Intent(TerminalLearningMaterialsActivity.this,
						TerminalLearningListActivity.class);
				
				String studyTypeId = mCurrentFirstEntity == null ? ""
						: mCurrentFirstEntity.id;
				intent.putExtra("studyTypeId", studyTypeId);
				
				String childTypeId = mCurrentSecondEntity == null ? ""
						: mCurrentSecondEntity.id;
				intent.putExtra("childTypeId", childTypeId);
				
				String time = mSelectTimeEntity == null ? ""
						: mSelectTimeEntity.dateType;
				intent.putExtra("time", time);
				
				intent.putExtra("themeWord", themeWordString);
				intent.putExtra("keyWord", keyWordString);
				startActivity(intent);
			}
		});

	}

	/**
	 * 初始化资料大类别下拉框
	 */
	private void initLearningTypePopupView() {
		seriesBooksBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListPopupView(v);
			}

			private void showListPopupView(View v) {
				initListPopupView();
				mTypeListPopupView.showAsDropDown(v);
			}

			private void initListPopupView() {

				mTypeListPopupView = new TerminalLearningTypeListPopupView(
						TerminalLearningMaterialsActivity.this,
						mCurrentFirstEntity, mSelf, 0, null);

			}

		});

	}

	/**
	 * 初始化子类别下拉框
	 */
	private void initChildTypeListPopupView() {

		childSeriesBooksBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListPopupView(v);

			}

			private void initListPopupView() {

				if (mCurrentFirstEntity == null) {// 不选择资料类别
					Toast.makeText(mSelf,
							getString(R.string.learning_materials_two_hint),
							Toast.LENGTH_LONG).show();
					return;
				}
				if (mChildTypeListPopupView == null) {
					mChildTypeListPopupView = new TerminalLearningTypeListPopupView(
							TerminalLearningMaterialsActivity.this,
							mCurrentSecondEntity, mSelf, 1,
							mCurrentFirstEntity.id);

				}
			}

			private void showListPopupView(View v) {
				initListPopupView();
				if (mChildTypeListPopupView != null) {
					mChildTypeListPopupView.showAsDropDown(v);
				}
			}
		});
	}

	/**
	 * 初始化时间下拉框
	 */
	private void initTerminalDataTimePopupView() {

		timeBooksBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showListPopupView(v);

			}

			private void showListPopupView(View v) {
				initListPopupView();
				mThirdPopupView.showAsDropDown(v);
			}

			private void initListPopupView() {

				mThirdPopupView = new TerminalDataTimePopupView(
						TerminalLearningMaterialsActivity.this,
						mSelectTimeEntity, mSelf);

			}
		});

	}

	/**
	 * 小类别名称
	 */
	public void initChildTypeBtn(AttrEntity currentFirstEntity) {
		if (currentFirstEntity == null) {
			return;
		}
		final Button childSeriesBooksBtn = (Button) findViewById(R.id.btn_child_series);
		childSeriesBooksBtn.setText(mCurrentSecondEntity.name);
	}

	public void initStudyChildTypeEntity(AttrEntity currentSecondEntity) {
		mCurrentSecondEntity = currentSecondEntity;
	}

	public void initStudyTypeEntity(AttrEntity currentFirstEntity) {
		mCurrentFirstEntity = currentFirstEntity;
	}

	/**
	 * 资料类别
	 * 
	 * @param currentSecondEntity
	 */
	public void initStudyTypeBtn(AttrEntity currentSecondEntity) {
		final Button seriesBooksBtn = (Button) findViewById(R.id.btn_series);
		seriesBooksBtn.setText(mCurrentFirstEntity.name);
	}

	/**
	 * 更新时间实体
	 * 
	 * @param currentThirdEntity
	 */
	public void updateThirdEntity(MessageTopThirdEntity currentThirdEntity) {
		mSelectTimeEntity = currentThirdEntity;

	}

	/**
	 * 更新时间下拉框显示选择的时间
	 * 
	 * @param currentThirdEntity
	 */
	public void updateThirdBtn(MessageTopThirdEntity currentThirdEntity) {
		Button thirdBtn = (Button) findViewById(R.id.btn_select_time);
		thirdBtn.setText(currentThirdEntity.name);
	}

	/**
	 * 清除小类别已选择值
	 */
	public void clearChildTypeBtn() {
		final Button childSeriesBooksBtn = (Button) findViewById(R.id.btn_child_series);
		childSeriesBooksBtn.setText("");
		mCurrentSecondEntity = null;
		mChildTypeListPopupView = null;
	}

	/**
	 * 点击边缘隐藏
	 */
	private void initClickEdgehide() {
		LinearLayout parentLyt = (LinearLayout) findViewById(R.id.lyt_parent);
		parentLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// setTopLayoutVisible();
				KeyboardUtil keyboardUtil = new KeyboardUtil();
				keyboardUtil.hideSoftKeyboard(v);
			}
		});
	}

	private boolean setTopLayoutVisible() {
		final LinearLayout topLyt = (LinearLayout) findViewById(R.id.lyt_top);
		if (topLyt.getVisibility() == View.GONE) {
			topLyt.setVisibility(View.VISIBLE);
			clearFocus();
			return true;
		}
		clearFocus();
		return false;
	}

	/**
	 * 点击返回键
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			/*
			 * if (setTopLayoutVisible()) { return true; }
			 */
			if(lyt_default_content.getVisibility() == View.VISIBLE){
				lyt_default_content.setVisibility(View.GONE);
				return false;
			}else{
				return super.onKeyDown(keyCode, event);	
			}
			
			
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private void clearFocus() {

		/**
		 * 使用EditText左侧的ImageView抢Focus
		 */
		View requestFocusTv = (View) findViewById(R.id.iv_request_focus);
		requestFocusTv.requestFocus();

		final EditText searchInputEt = (EditText) findViewById(R.id.et_input);
		searchInputEt.clearFocus();
	}


}
